{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "90032869-6580-4f6c-8ab6-d0bbc83f10df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print('hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "27d596be-2e48-4c69-9f4f-239ad13d545b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入圆的半径:  11\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "周长: 69.12\n",
      "面积: 380.13\n"
     ]
    }
   ],
   "source": [
    "radius = float(input('请输入圆的半径: '))\n",
    "perimeter = 2 * 3.1416 * radius\n",
    "area = 3.1416 * radius * radius\n",
    "print('周长: %.2f' % perimeter)\n",
    "print('面积: %.2f' % area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4d718265-3433-4188-8ffa-167d7eba08b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "x =  1.1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f(1.1) = -1.6999999999999997\n"
     ]
    }
   ],
   "source": [
    "x = float(input('x = '))\n",
    "if x > 1:\n",
    "    y = 3 * x - 5\n",
    "elif x >= -1:\n",
    "    y = x + 2\n",
    "else:\n",
    "    y = 5 * x + 3\n",
    "print(f'f({x}) = {y}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "884fac2f-3e5b-4bcf-a631-80f4f7262de5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world! 你好，世界！\n",
      "\n",
      "hello, \n",
      "world!\n"
     ]
    }
   ],
   "source": [
    "s1 = 'hello, world!'\n",
    "s2 = \"你好，世界！\"\n",
    "print(s1, s2)\n",
    "s3 = '''\n",
    "hello, \n",
    "world!\n",
    "'''\n",
    "print(s3, end='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c1818b1a-29df-4c4b-a666-6dcd957a8ac5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "s1 = 'hello'\n",
    "for ch in s1:\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8872086e-e363-46ec-863a-f90922ffc57b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 h\n",
      "1 e\n",
      "2 l\n",
      "3 l\n",
      "4 o\n"
     ]
    }
   ],
   "source": [
    "s1='hello'\n",
    "for i,j in enumerate(s1):\n",
    "    print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0bc66dbc-99f2-49b1-a62f-c65518c98fa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '王大锤', 'age': 55, 'weight': 60, 'home': '中同仁路8号'}\n"
     ]
    }
   ],
   "source": [
    "person = dict(name='王大锤', age=55, weight=60, home='中同仁路8号')\n",
    "print(person)    # {'name': '王大锤', 'age': 55, 'weight': 60, 'home': '中同仁路8号'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b51518cd-2fcc-42f9-9ffd-6dd4f4772b63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "name\n",
      "age\n",
      "weight\n",
      "office\n"
     ]
    }
   ],
   "source": [
    "person = {'name': '王大锤', 'age': 55, 'weight': 60, 'office': '科华北路62号'}\n",
    "print(len(person))    # 4\n",
    "for key in person:\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "24aa5182-3814-4e66-9fac-7d54ff50abd1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "m =  22\n",
      "n =  3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1124000727777607680000\n",
      "562000363888803840000\n",
      "187333454629601280000\n"
     ]
    }
   ],
   "source": [
    "m = int(input('m = '))\n",
    "n = int(input('n = '))\n",
    "fm = 1\n",
    "for num in range(1, m + 1):\n",
    "    fm *= num\n",
    "    fn = 1\n",
    "for num in range(1, n + 1):\n",
    "    fn *= num\n",
    "    print(fm // fn )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "65401feb-6f49-4f68-aa9a-cced71e6ba63",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import string\n",
    "\n",
    "ALL_CHARS = string.digits + string.ascii_letters\n",
    "def generate_code(code_len=4):\n",
    "    \"\"\"生成指定长度的验证码\n",
    "    \n",
    "    :param code_len: 验证码的长度(默认4个字符)\n",
    "    :return: 由大小写英文字母和数字构成的随机验证码字符串\n",
    "    \"\"\"\n",
    "    return ''.join(random.choices(ALL_CHARS, k=code_len))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a9721ba7-0b47-45e5-bcc7-2c81a3a478ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I1yL\n",
      "Be1F\n",
      "MuqH\n",
      "JKhF\n",
      "Sg8O\n",
      "UEXs\n",
      "4EgY\n",
      "F0x6\n",
      "Ygmc\n",
      "pVPt\n"
     ]
    }
   ],
   "source": [
    "for _ in range(10):\n",
    "    print(generate_code()) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8e48591c-4619-4fe5-bba5-453a8694f120",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_suffix(filename, ignore_dot=True):\n",
    "    \"\"\"获取文件名的后缀名\n",
    "    \n",
    "    :param filename: 文件名\n",
    "    :param ignore_dot: 是否忽略后缀名前面的点\n",
    "    :return: 文件的后缀名\n",
    "    \"\"\"\n",
    "  \n",
    "    pos = filename.rfind('.')\n",
    "    \n",
    "    if pos <= 0:\n",
    "        return ''\n",
    "    return filename[pos + 1:] if ignore_dot else filename[pos:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1ce29fa2-0cfa-439d-8910-9022447caff8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "txt\n",
      "md\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(get_suffix('readme.txt'))       \n",
    "print(get_suffix('readme.txt.md'))    \n",
    "print(get_suffix('.readme'))          \n",
    "print(get_suffix('readme.'))          \n",
    "print(get_suffix('readme'))           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b4ddee9d-c131-4b57-a343-304bb7424388",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_prime(num: int) -> bool:\n",
    "    \"\"\"判断一个正整数是不是质数\n",
    "\n",
    "    :param num: 正整数\n",
    "    :return: 如果是质数返回True，否则返回False\n",
    "    \"\"\"\n",
    "    for i in range(2, int(num ** 0.5) + 1):\n",
    "        if num % i == 0:\n",
    "            return False\n",
    "    return num != 1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
